import { _decorator, Color, Component, instantiate, Label, Node, Prefab, RichText, Sprite } from 'cc';
import { UIManager } from '../common/UIManager';
import { PrefabEnum } from '../common/PrefabEnum';
import { UserInfo } from '../common/UserInfo';
import { Utils } from '../common/Utils';
import { Building } from '../entity/Buildings';
import { BuildingBtnCheck, CheckResult } from './BuildingBtnCheck';
import { VillageController } from './VillageController';
import { Space } from './Space';
import { BuildingCon } from './BuildingCon';
import { EventManager } from '../event/EventManager';
const { ccclass, property } = _decorator;

@ccclass('BuildingDetail')
export class BuildingDetail extends Component {
    @property(Node)
    public mask: Node;
    @property(Node)
    public closeBtn: Node;
    @property(Sprite)
    public cover: Sprite;
    @property(Label)
    public buildName: Label;
    @property(Label)
    public level: Label;
    @property(Label)
    public desc: Label;
    @property(Label)
    public food: Label;
    @property(Label)
    public iron: Label;
    @property(Label)
    public mud: Label;
    @property(Label)
    public wood: Label;
    @property(Label)
    public time: Label;
    @property(RichText)
    public lack: RichText;
    @property(Label)
    public unlockTitle: Label
    @property(Node)
    public buildBtn: Node;
    @property(Node)
    buildingConLayer: Node
    @property(Node)
    conditionDiv:Node
    @property(Prefab)
    buildingCon: Prefab

    private data: Building
    private buildUse: boolean = false

    start() {
        this.mask.on(Node.EventType.TOUCH_END, this.onClose, this)
        this.closeBtn.on(Node.EventType.TOUCH_END, this.onClose, this)
        this.buildBtn.on(Node.EventType.TOUCH_END, this.onBuild, this)
    }

    public async setData(data: Building) {
        console.log(data)
        this.data = data
        

        this.buildName.string = data.name
        this.level.string = data.level + ''
        this.desc.string = data.desc

        this.food.string = Utils.bigNumberTransform(data.food)
        this.wood.string = Utils.bigNumberTransform(data.wood)
        this.mud.string = Utils.bigNumberTransform(data.mud)
        this.iron.string = Utils.bigNumberTransform(data.iron)

        this.cover.spriteFrame = await data.getImg()
        Building.setCoverSize(this.cover.node.parent, this.cover)

        let wood_reserve = UserInfo.instance.villageData.res.wood_reserve
        let mud_reserve = UserInfo.instance.villageData.res.mud_reserve
        let iron_reserve = UserInfo.instance.villageData.res.iron_reserve
        let food_reserve = UserInfo.instance.villageData.res.food_reserve


        if (wood_reserve < data.wood) {
            this.wood.color = new Color('#FD4545')
        }
        if (mud_reserve < data.mud) {
            this.mud.color = new Color('#FD4545')
        }
        if (iron_reserve < data.iron) {
            this.iron.color = new Color('#FD4545')
        }
        if (food_reserve < data.food) {
            this.food.color = new Color('#FD4545')
        }


        let speed = UserInfo.instance.speed
        this.time.string = Utils.secondToStr(Math.floor(data.time / speed))

        let needLevel = data.needLevel
        
        for (let item of needLevel) {
            let node = instantiate(this.buildingCon)
            this.buildingConLayer.addChild(node)
            node.getComponent(BuildingCon).setData(item)

            if (item.isLack) {
                this.buildBtn.active = false
            }
        }

        if(needLevel.length == 0){
            this.conditionDiv.active = false
        }
        // this.lack.string = data.needLevel.map(obj => {

        //     if(obj.isLack){
        //         // 如果其他建筑物未满足等级，就直接隐藏建造按钮
        //        

        //         return `<color=#000>${obj.name}</color><color=#FD4545>等级${obj.level}</color>`
        //     } else {
        //         return `<color=#000>${obj.name}等级${obj.level}</color>`
        //     }

        // }).join('，')
    }


    onClose() {
        UIManager.instance.closeUI(PrefabEnum.BuildingDetail)
    }

    async onBuild() {
        let lingBuilding = Building.createByIdAndLevel(this.data.id, 0)
        
        if(!lingBuilding.isEnoughConsumeForBuild){
            UIManager.instance.modal('提示', '粮食产量不足，请先升级农田', false)
            return
        }

        let result = BuildingBtnCheck.checkBuildBtn(this.data)

        if(result == CheckResult.NoWorker){
            UIManager.instance.openUI(PrefabEnum.WorkerList)
        }

        if (result != CheckResult.Pass) {
            
            return
        }


        if (this.data.id == 31 ||this.data.id == 32 || this.data.id == 33) {

            EventManager.instance.emit(EventManager.EventType.BuildWall, 40)
           
            // 关闭UI
            UIManager.instance.clear()
            return
        }

        if (VillageController.instance.curClickSpace) {
            let space = VillageController.instance.curClickSpace.getComponent(Space)
            await space.buildBuilding(this.data.id)
        } else {
            VillageController.instance.curBuilding = this.data
            VillageController.instance.openSpaceTip()
        }


        // 关闭UI
        UIManager.instance.clear()

    }

}


